package com.webex.ta.hydra.gui.menu;

import com.webex.ta.hydra.gui.action.ActionNames;
import com.webex.ta.hydra.gui.action.ActionRouter;
import com.webex.ta.hydra.gui.util.RolloverButton;
import com.webex.ta.hydra.gui.GuiPackage;
import com.webex.ta.hydra.gui.table.HydraTable;
import com.webex.ta.hydra.util.HydraUtils;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-27
 * Time: 9:40:45
 */
public class HydraToolBar extends JToolBar {
    protected RolloverButton newproject, open, save;

    protected RolloverButton undo, redo;

    protected RolloverButton run, debug, resume, pause, stop;

    protected RolloverButton config;

    protected RolloverButton help;

    public HydraToolBar() {
        super();
        this.setFloatable(false);
        this.setRollover(true);

        newproject = makeToolButton(ActionNames.NEW, HydraUtils.getImage("new.gif"));
        open = makeToolButton(ActionNames.OPEN, HydraUtils.getImage("prj_obj.gif"));
        save = makeToolButton(ActionNames.SAVE, HydraUtils.getImage("save.gif"));

        undo = makeToolButton(ActionNames.UNDO, HydraUtils.getImage("undo.gif"));
        undo.setEnabled(false);
        redo = makeToolButton(ActionNames.REDO, HydraUtils.getImage("redo.gif"));
        redo.setEnabled(false);

        run = makeToolButton(ActionNames.RUN, HydraUtils.getImage("run.gif"));
        debug = makeToolButton(ActionNames.DEBUG, HydraUtils.getImage("debug_exc.gif"));
        resume = makeToolButton(ActionNames.RESUME, HydraUtils.getImage("resume_co.gif"));
        stop = makeToolButton(ActionNames.STOP, HydraUtils.getImage("stop.gif"));
//        resume.setEnabled(false);
        pause = makeToolButton(ActionNames.PAUSE, HydraUtils.getImage("pause.gif"));
        pause.setEnabled(false);
        config = makeToolButton(ActionNames.CONFIG, HydraUtils.getImage("configs.gif"));

        help = makeToolButton(ActionNames.ABOUT, HydraUtils.getImage("help.gif"));

        add(newproject);
        add(open);
        add(save);
        addSeparator();
        add(undo);
        add(redo);
        addSeparator();
        add(run);
        add(debug);
        add(pause);
        add(resume);
        add(stop);
        add(config);
        addSeparator();
        add(help);
    }

    @Override
    public void addSeparator() {
        addSeparator(new Dimension(8,16));
    }

    public void addSeparator( Dimension size ) {
        Separator s = new Separator( size );
        add(s);
    }

    public void update() {
        HydraTable table = GuiPackage.getInstance().getHydraTable();
        if (table == null) {
            undo.setEnabled(false);
            redo.setEnabled(false);
        } else {
            undo.setEnabled(table.canUndo());
            redo.setEnabled(table.canRedo());
        }
    }


    public static RolloverButton makeToolButton(String actionName, Icon icon) {
        RolloverButton btn = new RolloverButton(icon);
        btn.setMargin(new Insets(1,1,1,1));
        btn.setBorderPainted(false);
        btn.addActionListener(ActionRouter.getInstance());
        btn.setActionCommand(actionName);
        btn.setFocusable(false);
        return btn;
    }

    public void testStart() {
        run.setEnabled(false);
        debug.setEnabled(false);
        pause.setEnabled(true);
        resume.setEnabled(false);
        stop.setEnabled(true);
    }

    public void testStop() {
        run.setEnabled(true);
        debug.setEnabled(true);
        pause.setEnabled(false);
        resume.setEnabled(false);
        stop.setEnabled(false);
    }

    public void testPaused() {
        run.setEnabled(false);
        debug.setEnabled(false);
        pause.setEnabled(false);
        resume.setEnabled(true);
        stop.setEnabled(true);
    }

    static public class Separator extends JToolBar.Separator {
        public Separator() {
            super();
        }

        public Separator(Dimension size) {
            super(size);
        }

        @Override
        public void paint(Graphics g) {
            Dimension d = this.getSize();
            g.setColor(Color.gray);
            g.drawLine(d.width/2, 0, d.width/2, d.height);
        }
    }
}
